/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.stakeholder;

import entity.AddressEntity;
import entity.BranchEntity;
//import entity.FnBBranchEntity;
//import entity.FnBIOTREntity;
//import entity.PackagePurchasedEntity;
//import entity.ServicePackageEntity;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
//import util.exception.ChangeServicePackageException;
//import util.exception.StakeholderBizRegNumberExist;
//import util.exception.StakeholderUsernameExist;
import java.util.List;
import entity.StakeholderEntity;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import javax.faces.event.PhaseEvent;
//import session.stateful.BranchSessionBean;
//import session.stateful.ServiceSessionBean;
import java.security.SecureRandom;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import session.stateless.BranchSessionBean;
import session.stateless.StakeholderSessionBeanStateless;
import exception.ExistException;
import session.stateless.SystemMessagesBean;
import util.enumeration.IndustryType;
import util.localization.WebLocalization;

/**
 *
 * @author jamie
 */
@ManagedBean
@SessionScoped
public class StakeholderRegisterManagedBean implements Serializable {

    @EJB
    private StakeholderSessionBeanStateless stakeholderSessionBean;
    @EJB
    private BranchSessionBean branchSessionBean;
    @EJB
    private SystemMessagesBean systemMessages;
    //@EJB
    //private ServiceSessionBean serviceSessionBean;
//    private String statusMessage;
    private Date dateExpired;
    private String oldPassword;
    private String newPassword;
    private String retypeNewPassword;
    private int numOfMonth;
    private int numOfMonthExtend;
    private AddressEntity stakeholderAddress = new AddressEntity();
    //private AddressEntity branchAddress = new AddressEntity();
    private StakeholderEntity selectedStakeholder;
    private StakeholderEntity stakeholder = new StakeholderEntity();
    // private FnBBranchEntity selectedBranch = new FnBBranchEntity();
    // private ServicePackageEntity selectedServicePackage;
    // private PackagePurchasedEntity currentServicePackage;
    //private List<IndustryTypeEntity> industryTypeList;
    private IndustryType industryType;
    // private List<FnBBranchEntity> fnBBranchList = new ArrayList<FnBBranchEntity>();
    private List<BranchEntity> branchList;
    // private FnBIOTREntity fnBIOTR = new FnBIOTREntity();
    private SecureRandom random = new SecureRandom();
    private String preferredLocale;
    private WebLocalization localization;

    public StakeholderRegisterManagedBean() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
    }

    public void register(ActionEvent event) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        if (getStakeholder().getCorpEmail().indexOf("@") == -1) {
            String statusMessage = getSystemMessages().getSystemMessage("STAKEHOLDER_EMAIL_CHECK", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, statusMessage, ""));
            return;

        }

        try {
            String generatedPassword = randomString();
            String encryptedPassword = encrypt(oldPassword);
            getStakeholder().setPassword(encryptedPassword);
            System.out.println("what is the industrytype" + stakeholder.getIndustry());
            getStakeholderSessionBean().addNewStakeholder(getStakeholder(), industryType, getStakeholderAddress());//getIndustryType());

//            setStatusMessage("New stakeholder added successfully");
            String statusMessage = getSystemMessages().getSystemMessage("STAKEHOLDER_CREATION_SUCCESS", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_INFO, statusMessage, ""));
        } catch (ExistException ex) {
            //String statusMessage = getSystemMessages().getSystemMessage("STAKEHOLDER_CONTACT_TCUBE", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
        }
    }

    public String randomString() {
        String generatedString = new BigInteger(100, random).toString(32);
        return generatedString;
    }

    public String encrypt(String inputString) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String outputString = util.service.PasswordService.getInstance().encrypt(inputString);
        return outputString;
    }

    public boolean validateSamePassword() {
        if (retypeNewPassword.equals(newPassword)) {
            return true;
        } else {
            return false;
        }
    }

    public void initIndustryType(PhaseEvent event) {

        /*LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */

        FacesContext.getCurrentInstance().getExternalContext().getSession(true);

        //Clear previous set messages(delete)
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();

        System.out.println("getting industry list...");
        //industryTypeList = new ArrayList<IndustryType>();
    }
    /*public void initIndustryType(PhaseEvent event) {
    System.out.println(stakeholderSessionBean == null);
    industryTypeList = stakeholderSessionBean.getIndustryTypeList();
    }*/

    public void retrieveStakeholderInfo() {
        this.setSelectedStakeholder(stakeholderSessionBean.retrieveStakeholderInfoByUsername(stakeholder.getUsername()));
    }

    public List<StakeholderEntity> getStakeholders() {
        return getStakeholderSessionBean().getAllStakeholders();
    }

    /**
     * @return the stakeholderSessionBean
     */
    public StakeholderSessionBeanStateless getStakeholderSessionBean() {
        return stakeholderSessionBean;
    }

    /**
     * @return the branchSessionBean
     */
    public BranchSessionBean getBranchSessionBean() {
        return branchSessionBean;
    }

    /**
     * @return the dateExpired
     */
    public Date getDateExpired() {
        return dateExpired;
    }

    /**
     * @return the oldPassword
     */
    public String getOldPassword() {
        return oldPassword;
    }

    /**
     * @return the newPassword
     */
    public String getNewPassword() {
        return newPassword;
    }

    /**
     * @return the retypeNewPassword
     */
    public String getRetypeNewPassword() {
        return retypeNewPassword;
    }

    /**
     * @return the numOfMonth
     */
    public int getNumOfMonth() {
        return numOfMonth;
    }

    /**
     * @return the numOfMonthExtend
     */
    public int getNumOfMonthExtend() {
        return numOfMonthExtend;
    }

    /**
     * @return the stakeholderAddress
     */
    public AddressEntity getStakeholderAddress() {
        return stakeholderAddress;
    }

    /**
     * @return the selectedStakeholder
     */
    public StakeholderEntity getSelectedStakeholder() {
        return selectedStakeholder;
    }

    /**
     * @return the stakeholder
     */
    public StakeholderEntity getStakeholder() {
        return stakeholder;
    }

    /**
     * @return the industryTypeList
     */
    public IndustryType getIndustryType() {
        return industryType;
    }

    /**
     * @return the branchList
     */
    public List<BranchEntity> getBranchList() {
        return branchList;
    }

    /**
     * @return the random
     */
    public SecureRandom getRandom() {
        return random;
    }

    /**
     * @param stakeholderSessionBean the stakeholderSessionBean to set
     */
    public void setStakeholderSessionBean(StakeholderSessionBeanStateless stakeholderSessionBean) {
        this.stakeholderSessionBean = stakeholderSessionBean;
    }

    /**
     * @param branchSessionBean the branchSessionBean to set
     */
    public void setBranchSessionBean(BranchSessionBean branchSessionBean) {
        this.branchSessionBean = branchSessionBean;
    }

    /**
     * @param dateExpired the dateExpired to set
     */
    public void setDateExpired(Date dateExpired) {
        this.dateExpired = dateExpired;
    }

    /**
     * @param oldPassword the oldPassword to set
     */
    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    /**
     * @param newPassword the newPassword to set
     */
    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    /**
     * @param retypeNewPassword the retypeNewPassword to set
     */
    public void setRetypeNewPassword(String retypeNewPassword) {
        this.retypeNewPassword = retypeNewPassword;
    }

    /**
     * @param numOfMonth the numOfMonth to set
     */
    public void setNumOfMonth(int numOfMonth) {
        this.numOfMonth = numOfMonth;
    }

    /**
     * @param numOfMonthExtend the numOfMonthExtend to set
     */
    public void setNumOfMonthExtend(int numOfMonthExtend) {
        this.numOfMonthExtend = numOfMonthExtend;
    }

    /**
     * @param selectedStakeholder the selectedStakeholder to set
     */
    public void setSelectedStakeholder(StakeholderEntity selectedStakeholder) {
        this.selectedStakeholder = selectedStakeholder;
    }

    /**
     * @param stakeholder the stakeholder to set
     */
    public void setStakeholder(StakeholderEntity stakeholder) {
        this.stakeholder = stakeholder;
    }

    /**
     * @param industryTypeList the industryTypeList to set
     */
    public void setIndustryType(IndustryType industryType) {
        this.industryType = industryType;
    }

    /**
     * @param branchList the branchList to set
     */
    public void setBranchList(List<BranchEntity> branchList) {
        this.branchList = branchList;
    }

    /**
     * @param random the random to set
     */
    public void setRandom(SecureRandom random) {
        this.random = random;
    }

    /**
     * @param stakeholderAddress the stakeholderAddress to set
     */
    public void setStakeholderAddress(AddressEntity stakeholderAddress) {
        this.stakeholderAddress = stakeholderAddress;
    }

    public WebLocalization getLocalization() {
        return localization;
    }

    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }

    public String getPreferredLocale() {
        return preferredLocale;
    }

    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }

    public SystemMessagesBean getSystemMessages() {
        return systemMessages;
    }

    public void setSystemMessages(SystemMessagesBean systemMessages) {
        this.systemMessages = systemMessages;
    }
}
